package com.cskaoyan.servlet;


import com.cskaoyan.util.ConfigPropertiesUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/wx/storage/fetch/*")
public class WxStorageFetchServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String substring = requestURI.substring(requestURI.lastIndexOf("/") + 1);
        String storagePath = ConfigPropertiesUtil.getValue("storage.path");
        System.out.println("11111111111111111111111111111111111111111");
        System.out.println(storagePath + substring);
        System.out.println("11111111111111111111111111111111111111111");
        FileInputStream fileInputStream = new FileInputStream(storagePath + substring);
        System.out.println(fileInputStream);
        ServletOutputStream outputStream = response.getOutputStream();

        int read;
        byte[] bytes = new byte[1024];
        while ((read = fileInputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    }
}
